package com.bjdd.jdy.baidu;

import android.content.Context;
import android.location.LocationManager;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.bjdd.jdy.App;
import com.bjdd.jdy.Constant;
import com.bjdd.jdy.base.RxManager;
import com.bjdd.jdy.util.L;
import com.bjdd.jdy.util.T;

import static android.content.Context.LOCATION_SERVICE;

/**
 * Created by Administrator on 2017/4/7.
 */

public class LocationUtils {
    private static volatile LocationUtils instance;
    private LocationClient mLocationClient = null;
    private LocationClient locationClient = null;
    public BDLocationListener listener,onceListener;
    private Context mContext;
    private Boolean isCommitLocation = true;
    private RxManager rxManager = new RxManager();
    public static double lng;
    public static double lat;
    public static BDLocation location;

    public static LocationUtils getInstance(Context context){
        if (instance == null){
            synchronized (LocationUtils.class){
                instance = new LocationUtils(context);
            }
            //instance = new LocationUtils();
        }
        return instance;
    }
    private LocationUtils(Context mContext){
        mLocationClient = new LocationClient(mContext);
        locationClient = new LocationClient(mContext);//只调用一次
        //定时调用 30秒调用一次
        int span = 1000 * 30;
        initLocation(mLocationClient,span);
        span = 1000;//只调用一次
        initLocation(locationClient,span);
        initListener();
        mLocationClient.registerLocationListener(listener);
        locationClient.registerLocationListener(onceListener);
    }
    private void initLocation(LocationClient client,int span){
        LocationClientOption option = new LocationClientOption();
        //可选，默认高精度，设置定位模式，高精度，低功耗，仅设备
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
        //可选，默认gcj02，设置返回的定位结果坐标系
        option.setCoorType("bd09ll");
        //可选，默认0，即仅定位一次，设置发起定位请求的间隔需要大于等于1000ms才是有效的
        option.setScanSpan(span);
        //可选，设置是否需要地址信息，默认不需要
        option.setIsNeedAddress(true);
        //可选，默认false，设置是否使用GPS
        option.setOpenGps(true);
        //可选，默认false，设置是否当GPS有效时按照1s/1次的频率输出GPS结果
        option.setLocationNotify(true);
        option.setIsNeedLocationDescribe(true);
        //可选，默认false，设置是否需要位置语义化结果，可以在BDLocation.getLocationDescribe里得到，结果类似于“在北京天安门附近”

        option.setIsNeedLocationPoiList(true);
        //可选，默认false，设置是否需要POI结果，可以在BDLocation.getPoiList里得到

        option.setIgnoreKillProcess(false);
        //可选，默认true，定位SDK内部是一个SERVICE，并放到了独立进程，设置是否在stop的时候杀死这个进程，默认不杀死

        option.SetIgnoreCacheException(false);
        //可选，默认false，设置是否收集CRASH信息，默认收集

        option.setEnableSimulateGps(false);
        //可选，默认false，设置是否需要过滤GPS仿真结果，默认需要

        client.setLocOption(option);
    }

    private void initListener(){
        listener = new BDLocationListener() {
            @Override
            public void onReceiveLocation(BDLocation bdLocation) {
                L.d("定位成功");
                lng = bdLocation.getLongitude();
                lat = bdLocation.getLatitude();
                location = bdLocation;
                rxManager.post(Constant.EVENT_LOCATION,bdLocation);
            }

            @Override
            public void onConnectHotSpotMessage(String s, int i) {
                L.e("定位错误---描述："+s+"---错误码："+i);
            }
        };
        onceListener = new BDLocationListener() {
            @Override
            public void onReceiveLocation(BDLocation bdLocation) {
                rxManager.post(Constant.EVENT_LOCATION_ONCE,bdLocation);
                lng = bdLocation.getLongitude();
                lat = bdLocation.getLatitude();
                location = bdLocation;
                locationClient.stop();
            }

            @Override
            public void onConnectHotSpotMessage(String s, int i) {

            }
        };
    }

    public void start(){
       /* if (!App.getApp().hasLocation()){
            T.show("请检查是否拥有定位权限");
            return;
        }*/
        if (!mLocationClient.isStarted()){
            mLocationClient.start();
        }
    }
    public void startOnce(){
        /*if (!App.getApp().hasLocation()){
            T.show("请检查是否拥有定位权限");
            return;
        }*/
        if (!locationClient.isStarted()){
            locationClient.start();
        }
    }
    public Boolean getCommitLocation() {
        return isCommitLocation;
    }

    public void setCommitLocation(Boolean commitLocation) {
        isCommitLocation = commitLocation;
    }

    public void stop(){
        if (mLocationClient.isStarted()){
            mLocationClient.stop();
        }
    }
}
